<?php
/**
 * Image service
 * @author xinqiyang
 *
 */
class ImageService extends BaseService
{

	public static function getImages($image_id,$icon='s',$thumb=true,$remote=false)
	{
		if(!($image_id > 0) && !empty($image_id))
		{
			return $image_id;
		}
		$url ='';
		$type = '48';
		$image = self::redisGetHashes($image_id);
		$label = $thumb ? '/spic/' : '/mpic/';
		switch ($icon){
			case 's':
				$type = '48';
				break;
			case 'm':
				$type = '100';
				break;
			case 'l':
				$type = '160';
				break;
			case 'z':
				$type = '500';
				break;
			default:
				$type = '48';
				break;
		}
		
		//$remoteurl = $remote ? '' : '';
		$url = is_array($image) ? ('/images/image/'.$type.'/'.$image['filename'].'.jpg') : "/images/default/$type/1.jpg";
		$url = C('IMGURL').$url;
		return $url;
	}

	
	public static function saveImage()
	{
		//one or more servers support
		//add image url
		try{
			$imagePath = C('image.image');
			$id = objid();
			$filename = $id.'.jpg';
			$path = $imagePath['path'].'image/'.$filename;
			logTrace(__CLASS__.'/'.__FUNCTION__."id:$id,filename=$filename,path=$path");
			
			$r = move_uploaded_file($_FILES['bigAvatar']['tmp_name'],$path);
			logTrace(__CLASS__.'/'.__FUNCTION__."move result:$r");
			//use resize image
			Image::resizeImg($path,'image',$id);
			return $id;
		}catch (MException $e){
			logWarning(__CLASS__.'/'.__FUNCTION__.':'.$e->__toString());
			return false;
		}
	}
	
	
	
}
